home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: Memory allocation.
- Date: 16 Feb 1996 19:35:44 GMT
- Organization: Borland International
- Message-ID: <4g2mag$f81@druid.borland.com>
- References: <1996Feb10.161530.26449@wisipc.weizmann.ac.il>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <1996Feb10.161530.26449@wisipc.weizmann.ac.il>, cerlpvk says...
- >
- >I'm a starter so the question may seem stupid.
- >
- >Would someone explain why this program works?
-
- Luck. Don't waste time understanding it. It is not legal code, because func()
- returns a pointer to an auto object. The compiler I use generates a warning on
- the return statement in func().
-
- >
- >int* func()
- >{
- >
- >
- > int b[10];
- > for(int i=0;i<9;i++)
- > b[i]=i;
- > return b;
- >}
- >
- >int main(){
- >
- > int* x= func();
- >
- >
- > cout<<x[2]<<endl;
- > return(1);
- >}
- >
- >The point is that b is allocated on the stack i.e without "new".
- >
- >Thanks in advance.
- >
-
-